package ro.gateway.aida.db;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Hashtable;


/**
 * <p>Title: Romanian AIDA</p> <p>Description: </p> <p>Copyright: Copyright (comparator) 2003</p> <p>Company:
 * ro-gateway</p>
 *
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: DBFilesPersMan.java,v 1.1 2004/10/24 23:37:09 mihaipostelnicu Exp $
 */
public class DBFilesPersMan extends DBPersistenceManager {

     private DBFilesPersMan( PersistenceToken token ) {
          super( token );
     }

     public static DBFilesPersMan getManager( PersistenceToken token ) {
          return new DBFilesPersMan( token );
     }

     public void deleteFile( String path ) throws SQLException {
          Connection con = getConnection();

          try {
               PreparedStatement ps = con.prepareStatement( "DELETE FROM project_files WHERE r_path=?" );
               ps.setString( 1, path );
               ps.executeUpdate();
               ps.close();
          } catch ( SQLException ex ) {
               throw ex;
          } finally {
               con.close();
          }
     }

     public void deleteTemporaryFiles() throws SQLException {
          Connection con = getConnection();

          try {
               PreparedStatement ps = con.prepareStatement( "DELETE FROM project_files WHERE published=0" );
               ps.executeUpdate();
               ps.close();
          } catch ( SQLException ex ) {
               throw ex;
          } finally {
               con.close();
          }
     }

     public void saveFile( String path, File file )
        throws SQLException, IOException {
          if ( exists( path ) ) throw new SQLException( "File exists in bd" );

          Connection con = getConnection();
          FileInputStream fis = new FileInputStream( file );

          try {
               PreparedStatement ps = con.prepareStatement( "INSERT INTO project_files (r_path, bdata, published) VALUES (?,?,0)" );
               ps.setString( 1, path );
               ps.setBinaryStream( 2, fis, ( int ) file.length() );
               ps.executeUpdate();
               ps.close();
               fis.close();
          } catch ( SQLException ex ) {
               throw ex;
          } catch ( IOException ioex ) {
               throw ioex;
          } finally {
               con.close();
          }
     }

     public void publishFile( String path )
        throws SQLException, IOException {

          Connection con = getConnection();

          try {
               PreparedStatement ps = con.prepareStatement( "UPDATE project_files SET published=1 WHERE r_path=?" );
               ps.setString( 1, path );
               ps.executeUpdate();
               ps.close();
          } catch ( SQLException ex ) {
               throw ex;
          } finally {
               con.close();
          }
     }

     public boolean exists( String path ) throws SQLException {
          Connection con = getConnection();

          int result = 0;
          try {
               PreparedStatement ps = con.prepareStatement( "SELECT count(1) FROM project_files WHERE r_path=?" );
               ps.setString( 1, path );
               ResultSet rs = ps.executeQuery();
               if ( rs.next() ) {
                    result = rs.getInt( 1 );
               }
               rs.close();
               ps.close();
          } catch ( SQLException sqlex ) {
               throw sqlex;
          } finally {
               con.close();
          }

          return result > 0;
     }

     private static String[][] CTYPES = new String[][]{
          {"zip", "application/zip"},
          {"doc", "application/msword"},
          {"pdf", "application/pdf"},
          {"xls", "application/vnd.ms-excel"},
          {"rtf", "application/rtf"},
          {"zip", "image/gif"},
          {"jpg", "image/jpeg"},
          {"jpeg", "image/jpeg"},
          {"gif", "image/gif"},
          {"png", "image/png"},
          {"bmp", "image/bmp"},
          {"tiff", "image/tiff"},
          {"html", "text/html"},
          {"txt", "text/plain"},
          {"xml", "text/xml"},
          {"mpeg", "video/mpeg"},
          {"mpg", "video/mpeg"},
          {"mpe", "video/mpeg"},
          {"avi", "video/avi"}
     };
     private static Hashtable HCTYPES;

     static {
          HCTYPES = new Hashtable();
          for ( int i = 0; i < CTYPES.length; i++ ) {
               HCTYPES.put( CTYPES[ i ][ 0 ], CTYPES[ i ][ 1 ] );
          }
     }

     public static String getContentTypeForFile( String file_name ) {
          if ( file_name == null ) return null;
          int lpoint = file_name.lastIndexOf( "." );
          String result = "application/zip";
          if ( lpoint == -1 ) {
               return result;
          }
          String extension = file_name.substring( lpoint + 1 );
          if ( extension == null || extension.length() < 1 ) {
               return result;
          }

//            String x = ( String ) HCTYPES.get( extension );
          String x = "application";
          if ( x != null ) result = x;

          return result;
     }
}
